<?php

/**
 *
 * HTTP header controller
 * 
 */
class HTTP_Header_Util
{
    // {{{ properties
	
	private static $headers = array(
		'content-type'  =>  'text/html',
        'pragma'        =>  'no-cache',
        'cache-control' =>  'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
	);

	// }}}
	// {{{ set()
	
	/**
     * Set Header
     * 
     * The default value for the Last-Modified header will be current
     * date and atime if $value is omitted.
     * 
     * @access  public
     * @return  bool    Returns true on success or false if $key was empty or
     *                  $value was not of an scalar type.
     * @param   string  $key The name of the header.
     * @param   string  $value The value of the header. (NULL to unset header)
     */
    public static function set($key, $value = null)
    {
        if (empty($key) || (isset($value) && !is_scalar($value))) {
            return false;
        }
        
        $key = strtolower($key);
        if ($key == 'last-modified') {
            if (!isset($value)) {
                $value = gmdate('D, d M Y H:i:s').' GMT';
            } elseif (is_numeric($value)) { 
                $value = gmdate ('D, d M Y H:i:s', $value).' GMT';
            }
        }
        
        if (isset($value)) {
            self::$headers[$key] = $value;
        } else {
            unset(self::$headers[$key]);
        }
        return true;
    }
    
    // }}}
    // {{{ send()
    
    /**
     * Send Headers
     * 
     * Send out the header that you set via setHeader().
     * 
     * @return  bool    Returns true on success or false if headers are already
     *                  sent.
     * @param   array   $keys Headers to (not) send, see $include.
     * @param   array   $include If true only $keys matching headers will be
     *                  sent, if false only header not matching $keys will be
     *                  sent.
     */
    public static function send($keys = array(), $include = true)
    {
        if (headers_sent($filename, $linenum)) {
            echo " Headers already sent in {$filename} on line {$linenum} \n";
            exit;
            //return false;
        }
        
        if (count($keys)) {
            array_change_key_case($keys, CASE_LOWER);
            foreach (self::$headers as $key => $value) {
                if ($include ? in_array($key, $keys) : ! in_array($key, $keys)) {
                    header($key .': '. $value);
                }
            }
        } else {
            foreach (self::$headers as $header => $value) {
                header($header .': '. $value);
            }
        }
        return true;
    }
    
    // }}}
	
	// {{{ setCharset()
	
	/**
	 * 标识输出内容
	 * 
	 * @param string $contentType
	 * @param string $charset
	 * @return void
	 */
	public static function setCharset($charset="utf-8", $contentType="text/html")
 	{
		header("Content-type:".$contentType.";charset=".$charset);
 	}
 	
	// }}}
	// {{{ Location()
	
	/**
	 * 重定向浏览器
	 * 
	 * @param string $url
	 * @retun void
	 */
	public static function Location($url)
	{
		header("Location:".$url);
		exit;
	}
	
	// }}}
	// {{{ downFileInfo()
	
	/**
	 * 待下载文件信息
	 * @param array $data
	 * @return void
	 */
	public static function downFileInfo(array $data=null)
	{
		// header
		header("Cache-Control: cache, must-revalidate");    
		header("Pragma: public");
		if(!isset($data['Content-type'])){
			$data['Content-type'] = "text/plain";
		}
		header("Content-type: ".$data['Content-type']);
		//header("Accept-Ranges: bytes");
		if(isset($data["size"])){
			header("Accept-Length: ".$data["size"]);
		}
		//header("Content-Transfer-Encoding: binary");
		if(!isset($data["filename"])){
			$data["filename"] = time().".txt";
		}
		header("Content-Disposition: attachment; filename=".$data["filename"]);
	}
	// }}}
}
?>